import 'package:flutter/material.dart';
import '../../pages/home_page.dart';
import '../../pages/web_view_page.dart';
import '../../pages/auth/login_page.dart';
import '../../pages/auth/register_page.dart';
import '../../pages/address/address_list_page.dart';
import '../../pages/address/add_edit_address_page.dart';
import '../../models/address_model.dart';
import '../../pages/product/product_detail_page.dart';
import '../../pages/order/order_detail_page.dart';

class AppRouter {
  // 路由生成方法
  static Route<dynamic> generateRoute(RouteSettings settings) {
    final args = settings.arguments;

    switch (settings.name) {
      case RouterPath.home:
        return _buildRoute(const HomePage(), settings: settings);
      case RouterPath.webViewPage:
        final url = args is String ? args : "https://www.baidu.com";
        return _buildRoute(WebViewPage(url: url), settings: settings);
      case RouterPath.login:
        return MaterialPageRoute(builder: (context) => const LoginPage());
      case RouterPath.register:
        return MaterialPageRoute(builder: (context) => const RegisterPage());
      case RouterPath.addresses:
        return MaterialPageRoute(builder: (_) => const AddressListPage());
      case RouterPath.addAddress:
        return MaterialPageRoute(builder: (_) => const AddEditAddressPage());
      case RouterPath.editAddress:
        final address = settings.arguments as Address;
        return MaterialPageRoute(
          builder: (_) => AddEditAddressPage(address: address),
        );
      case RouterPath.orderDetail:
        final orderId = settings.arguments as String;
        return MaterialPageRoute(
          builder: (_) => OrderDetailPage(orderId: orderId),
        );
      case RouterPath.productDetail:
        final productId = settings.arguments as String;
        return MaterialPageRoute(
          builder: (_) => ProductDetailPage(productId: productId),
        );
      default:
        return _buildRoute(
          const Scaffold(body: Center(child: Text('页面不存在'))),
          settings: settings,
        );
    }
  }

  // 统一页面路由构造方法
  static MaterialPageRoute<T> _buildRoute<T>(
    Widget page, {
    required RouteSettings settings,
    bool maintainState = true,
    bool fullscreenDialog = false,
    bool allowSnapshotting = true,
  }) {
    return MaterialPageRoute<T>(
      builder: (_) => page,
      settings: settings,
      maintainState: maintainState,
      fullscreenDialog: fullscreenDialog,
      allowSnapshotting: allowSnapshotting,
    );
  }

  // 路由跳转快捷方法
  static Future<T?> push<T extends Object?>(
    BuildContext context,
    String routeName, {
    Object? arguments,
  }) {
    return Navigator.of(context).pushNamed<T>(routeName, arguments: arguments);
  }

  // 替换当前路由（修正后的版本）
  static Future<T?> pushReplacement<T extends Object?, TO extends Object?>(
    BuildContext context,
    String routeName, {
    Object? arguments,
    TO? result,
  }) {
    return Navigator.of(
      context,
    ).pushReplacementNamed<T, TO>(routeName, arguments: arguments);
  }

  // 返回上一页
  static void pop<T extends Object?>(BuildContext context, [T? result]) {
    Navigator.of(context).pop<T>(result);
  }
}

// 路由路径常量
class RouterPath {
  static const String home = '/';
  static const String webViewPage = '/web_view';
  static const String login = '/login';
  static const String register = '/register';
  static const String addresses = '/addresses';
  static const String addAddress = '/addresses/add';
  static const String editAddress = '/addresses/edit';
  static const String productDetail = '/product-detail';
  static const String orderDetail = '/order-detail';
}
